<template>
  <div class="exam-result">
    <!-- 结果头部 -->
    <div class="result-header">
      <div class="header-content">
        <div class="result-status" :class="result.passed ? 'passed' : 'failed'">
          <div class="status-icon">
            <el-icon v-if="result.passed"><CircleCheck /></el-icon>
            <el-icon v-else><CircleClose /></el-icon>
          </div>
          <div class="status-text">
            <h1>{{ result.passed ? '恭喜通过！' : '未通过考试' }}</h1>
            <p>{{ result.passed ? '您已成功通过本次考试' : '继续努力，下次一定能通过' }}</p>
          </div>
        </div>
        
        <div class="result-score">
          <div class="score-circle">
            <div class="score-number">{{ result.score }}</div>
            <div class="score-total">/ 100</div>
          </div>
          <div class="score-info">
            <div class="score-label">总分</div>
            <div class="pass-line">及格线：{{ result.passingScore }}分</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 详细统计 -->
    <div class="result-stats">
      <div class="stats-container">
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Document /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.totalQuestions }}</div>
            <div class="stat-label">总题数</div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon correct">
            <el-icon><Check /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.correctAnswers }}</div>
            <div class="stat-label">答对题数</div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon wrong">
            <el-icon><Close /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.wrongAnswers }}</div>
            <div class="stat-label">答错题数</div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.timeUsed }}</div>
            <div class="stat-label">用时</div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.accuracy }}%</div>
            <div class="stat-label">正确率</div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Trophy /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ result.ranking }}</div>
            <div class="stat-label">排名</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 详细分析 -->
    <div class="result-analysis">
      <div class="analysis-container">
        <div class="analysis-left">
          <!-- 题型分析 -->
          <div class="analysis-section">
            <h3>题型分析</h3>
            <div class="type-analysis">
              <div 
                v-for="type in result.typeAnalysis" 
                :key="type.name"
                class="type-item"
              >
                <div class="type-header">
                  <span class="type-name">{{ type.name }}</span>
                  <span class="type-score">{{ type.score }}/{{ type.totalScore }}分</span>
                </div>
                <div class="type-progress">
                  <el-progress 
                    :percentage="type.percentage" 
                    :color="getProgressColor(type.percentage)"
                    :show-text="false"
                    :stroke-width="8"
                  />
                  <span class="progress-text">{{ type.percentage }}%</span>
                </div>
                <div class="type-details">
                  <span>答对{{ type.correct }}题，答错{{ type.wrong }}题</span>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 知识点分析 -->
          <div class="analysis-section">
            <h3>知识点掌握情况</h3>
            <div class="knowledge-analysis">
              <div 
                v-for="knowledge in result.knowledgeAnalysis" 
                :key="knowledge.name"
                class="knowledge-item"
              >
                <div class="knowledge-header">
                  <span class="knowledge-name">{{ knowledge.name }}</span>
                  <el-tag 
                    :type="getKnowledgeLevel(knowledge.level)" 
                    size="small"
                  >
                    {{ knowledge.level }}
                  </el-tag>
                </div>
                <div class="knowledge-progress">
                  <el-progress 
                    :percentage="knowledge.mastery" 
                    :color="getProgressColor(knowledge.mastery)"
                    :show-text="false"
                    :stroke-width="6"
                  />
                  <span class="progress-text">{{ knowledge.mastery }}%</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <div class="analysis-right">
          <!-- 成绩分布 -->
          <div class="analysis-section">
            <h3>成绩分布</h3>
            <div class="score-distribution">
              <div class="distribution-chart">
                <div class="chart-placeholder">
                  <el-icon><TrendCharts /></el-icon>
                  <span>成绩分布图</span>
                </div>
              </div>
              <div class="distribution-info">
                <div class="info-item">
                  <span class="label">您的分数：</span>
                  <span class="value">{{ result.score }}分</span>
                </div>
                <div class="info-item">
                  <span class="label">平均分：</span>
                  <span class="value">{{ result.averageScore }}分</span>
                </div>
                <div class="info-item">
                  <span class="label">最高分：</span>
                  <span class="value">{{ result.highestScore }}分</span>
                </div>
                <div class="info-item">
                  <span class="label">超越：</span>
                  <span class="value highlight">{{ result.beatPercentage }}%的考生</span>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 建议 -->
          <div class="analysis-section">
            <h3>学习建议</h3>
            <div class="suggestions">
              <div 
                v-for="suggestion in result.suggestions" 
                :key="suggestion.id"
                class="suggestion-item"
              >
                <div class="suggestion-icon">
                  <el-icon><Lightbulb /></el-icon>
                </div>
                <div class="suggestion-content">
                  <div class="suggestion-title">{{ suggestion.title }}</div>
                  <div class="suggestion-desc">{{ suggestion.description }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 错题回顾 -->
    <div class="wrong-questions" v-if="result.wrongQuestions.length > 0">
      <div class="section-header">
        <h3>错题回顾</h3>
        <span class="question-count">共{{ result.wrongQuestions.length }}题</span>
      </div>
      
      <div class="questions-list">
        <div 
          v-for="(question, index) in result.wrongQuestions" 
          :key="question.id"
          class="question-item"
        >
          <div class="question-header">
            <span class="question-number">第{{ question.number }}题</span>
            <el-tag type="danger" size="small">答错</el-tag>
          </div>
          
          <div class="question-content">
            <div class="question-title">{{ question.title }}</div>
            
            <div class="question-options">
              <div 
                v-for="option in question.options" 
                :key="option.key"
                class="option-item"
                :class="{
                  correct: option.isCorrect,
                  wrong: option.key === question.userAnswer && !option.isCorrect,
                  selected: option.key === question.userAnswer
                }"
              >
                <span class="option-key">{{ option.key }}.</span>
                <span class="option-text">{{ option.text }}</span>
                <span v-if="option.isCorrect" class="option-mark correct">✓</span>
                <span v-else-if="option.key === question.userAnswer" class="option-mark wrong">✗</span>
              </div>
            </div>
            
            <div class="question-analysis">
              <div class="analysis-title">解析：</div>
              <div class="analysis-content">{{ question.analysis }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="result-actions">
      <div class="actions-container">
        <el-button @click="viewDetailReport">
          <el-icon><Document /></el-icon>
          查看详细报告
        </el-button>
        
        <el-button @click="downloadCertificate" v-if="result.passed">
          <el-icon><Medal /></el-icon>
          下载证书
        </el-button>
        
        <el-button @click="retakeExam">
          <el-icon><Refresh /></el-icon>
          重新考试
        </el-button>
        
        <el-button @click="shareResult">
          <el-icon><Share /></el-icon>
          分享成绩
        </el-button>
        
        <el-button type="primary" @click="backToExamList">
          <el-icon><ArrowLeft /></el-icon>
          返回考试列表
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { 
  CircleCheck, CircleClose, Document, Check, Close, Clock,
  TrendCharts, Trophy, Lightbulb, Medal, Refresh, Share, ArrowLeft
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()

// 模拟考试结果数据
const result = ref({
  examId: 1,
  examTitle: 'JavaScript基础能力测试',
  score: 85,
  passingScore: 60,
  passed: true,
  totalQuestions: 50,
  correctAnswers: 42,
  wrongAnswers: 8,
  timeUsed: '45分钟',
  accuracy: 84,
  ranking: '前20%',
  averageScore: 72,
  highestScore: 98,
  beatPercentage: 78,
  typeAnalysis: [
    {
      name: '单选题',
      score: 28,
      totalScore: 30,
      percentage: 93,
      correct: 14,
      wrong: 1
    },
    {
      name: '多选题',
      score: 24,
      totalScore: 30,
      percentage: 80,
      correct: 8,
      wrong: 2
    },
    {
      name: '判断题',
      score: 18,
      totalScore: 20,
      percentage: 90,
      correct: 9,
      wrong: 1
    },
    {
      name: '填空题',
      score: 15,
      totalScore: 20,
      percentage: 75,
      correct: 3,
      wrong: 2
    }
  ],
  knowledgeAnalysis: [
    {
      name: '基础语法',
      level: '熟练',
      mastery: 90
    },
    {
      name: '函数与对象',
      level: '良好',
      mastery: 85
    },
    {
      name: 'DOM操作',
      level: '一般',
      mastery: 70
    },
    {
      name: '异步编程',
      level: '待提高',
      mastery: 60
    }
  ],
  suggestions: [
    {
      id: 1,
      title: '加强DOM操作练习',
      description: '建议多做一些DOM操作的实战练习，提高对DOM API的熟练度'
    },
    {
      id: 2,
      title: '深入学习异步编程',
      description: '重点学习Promise、async/await等异步编程概念和应用'
    },
    {
      id: 3,
      title: '巩固基础知识',
      description: '继续保持对基础语法的掌握，多做相关练习题'
    }
  ],
  wrongQuestions: [
    {
      id: 1,
      number: 15,
      title: '以下哪个方法可以用来阻止事件冒泡？',
      userAnswer: 'A',
      options: [
        { key: 'A', text: 'preventDefault()', isCorrect: false },
        { key: 'B', text: 'stopPropagation()', isCorrect: true },
        { key: 'C', text: 'stopImmediatePropagation()', isCorrect: false },
        { key: 'D', text: 'cancelBubble()', isCorrect: false }
      ],
      analysis: 'stopPropagation()方法用于阻止事件在DOM树中的传播，即阻止事件冒泡。preventDefault()用于阻止默认行为。'
    }
  ]
})

// 方法
const getProgressColor = (percentage) => {
  if (percentage >= 90) return '#67c23a'
  if (percentage >= 80) return '#409eff'
  if (percentage >= 60) return '#e6a23c'
  return '#f56c6c'
}

const getKnowledgeLevel = (level) => {
  const types = {
    '熟练': 'success',
    '良好': 'primary',
    '一般': 'warning',
    '待提高': 'danger'
  }
  return types[level] || 'info'
}

const viewDetailReport = () => {
  ElMessage.success('正在生成详细报告...')
}

const downloadCertificate = () => {
  ElMessage.success('正在下载证书...')
}

const retakeExam = () => {
  router.push(`/exam/detail/${result.value.examId}`)
}

const shareResult = () => {
  ElMessage.success('成绩分享链接已复制到剪贴板')
}

const backToExamList = () => {
  router.push('/exam/list')
}

onMounted(() => {
  // 根据路由参数加载考试结果
  const resultId = route.params.id
  if (resultId) {
    // 加载考试结果数据
  }
})
</script>

<style lang="scss" scoped>
.exam-result {
  min-height: 100vh;
  background: var(--el-bg-color-page);
  padding: 20px;
}

.result-header {
  background: white;
  border-radius: 12px;
  padding: 40px;
  margin-bottom: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 30px;
    text-align: center;
  }
}

.result-status {
  display: flex;
  align-items: center;
  gap: 20px;
  
  &.passed {
    .status-icon {
      color: var(--el-color-success);
    }
  }
  
  &.failed {
    .status-icon {
      color: var(--el-color-danger);
    }
  }
  
  .status-icon {
    font-size: 60px;
  }
  
  .status-text {
    h1 {
      font-size: 32px;
      font-weight: 700;
      margin-bottom: 8px;
      color: var(--el-text-color-primary);
    }
    
    p {
      font-size: 16px;
      color: var(--el-text-color-secondary);
      margin: 0;
    }
  }
}

.result-score {
  display: flex;
  align-items: center;
  gap: 20px;
}

.score-circle {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--el-color-primary) 0%, var(--el-color-primary-light-3) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  
  .score-number {
    font-size: 36px;
    font-weight: 700;
  }
  
  .score-total {
    font-size: 14px;
    opacity: 0.8;
  }
}

.score-info {
  .score-label {
    font-size: 14px;
    color: var(--el-text-color-secondary);
    margin-bottom: 4px;
  }
  
  .pass-line {
    font-size: 12px;
    color: var(--el-text-color-placeholder);
  }
}

.result-stats {
  margin-bottom: 24px;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  display: flex;
  align-items: center;
  gap: 16px;
  
  .stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--el-color-primary-light-9);
    color: var(--el-color-primary);
    font-size: 24px;
    
    &.correct {
      background: var(--el-color-success-light-9);
      color: var(--el-color-success);
    }
    
    &.wrong {
      background: var(--el-color-danger-light-9);
      color: var(--el-color-danger);
    }
  }
  
  .stat-content {
    .stat-number {
      font-size: 24px;
      font-weight: 700;
      color: var(--el-text-color-primary);
      margin-bottom: 4px;
    }
    
    .stat-label {
      font-size: 12px;
      color: var(--el-text-color-secondary);
    }
  }
}

.result-analysis {
  margin-bottom: 24px;
}

.analysis-container {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
  
  @media (max-width: 1024px) {
    grid-template-columns: 1fr;
  }
}

.analysis-section {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  margin-bottom: 20px;
  
  h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--el-text-color-primary);
  }
}

.type-analysis,
.knowledge-analysis {
  .type-item,
  .knowledge-item {
    margin-bottom: 20px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.type-header,
.knowledge-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  
  .type-name,
  .knowledge-name {
    font-weight: 500;
    color: var(--el-text-color-primary);
  }
  
  .type-score {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.type-progress,
.knowledge-progress {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
  
  .el-progress {
    flex: 1;
  }
  
  .progress-text {
    font-size: 12px;
    color: var(--el-text-color-secondary);
    min-width: 40px;
  }
}

.type-details {
  font-size: 12px;
  color: var(--el-text-color-placeholder);
}

.score-distribution {
  .distribution-chart {
    height: 200px;
    background: var(--el-fill-color-extra-light);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
    
    .chart-placeholder {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 8px;
      color: var(--el-text-color-placeholder);
      
      .el-icon {
        font-size: 32px;
      }
    }
  }
  
  .distribution-info {
    .info-item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 8px;
      
      .label {
        color: var(--el-text-color-secondary);
      }
      
      .value {
        font-weight: 500;
        color: var(--el-text-color-primary);
        
        &.highlight {
          color: var(--el-color-primary);
        }
      }
    }
  }
}

.suggestions {
  .suggestion-item {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .suggestion-icon {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      background: var(--el-color-warning-light-9);
      color: var(--el-color-warning);
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }
    
    .suggestion-content {
      .suggestion-title {
        font-weight: 500;
        margin-bottom: 4px;
        color: var(--el-text-color-primary);
      }
      
      .suggestion-desc {
        font-size: 14px;
        color: var(--el-text-color-secondary);
        line-height: 1.5;
      }
    }
  }
}

.wrong-questions {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  h3 {
    font-size: 18px;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
  
  .question-count {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.questions-list {
  .question-item {
    border: 1px solid var(--el-border-color-lighter);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 16px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  
  .question-number {
    font-weight: 500;
    color: var(--el-text-color-primary);
  }
}

.question-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 16px;
  color: var(--el-text-color-primary);
}

.question-options {
  margin-bottom: 16px;
  
  .option-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 6px;
    margin-bottom: 8px;
    position: relative;
    
    &.correct {
      background: var(--el-color-success-light-9);
      border: 1px solid var(--el-color-success-light-5);
    }
    
    &.wrong {
      background: var(--el-color-danger-light-9);
      border: 1px solid var(--el-color-danger-light-5);
    }
    
    .option-key {
      font-weight: 500;
      min-width: 20px;
    }
    
    .option-text {
      flex: 1;
    }
    
    .option-mark {
      font-weight: 700;
      
      &.correct {
        color: var(--el-color-success);
      }
      
      &.wrong {
        color: var(--el-color-danger);
      }
    }
  }
}

.question-analysis {
  background: var(--el-fill-color-extra-light);
  padding: 16px;
  border-radius: 6px;
  
  .analysis-title {
    font-weight: 500;
    margin-bottom: 8px;
    color: var(--el-text-color-primary);
  }
  
  .analysis-content {
    font-size: 14px;
    color: var(--el-text-color-secondary);
    line-height: 1.5;
  }
}

.result-actions {
  max-width: 1200px;
  margin: 0 auto;
}

.actions-container {
  display: flex;
  justify-content: center;
  gap: 16px;
  flex-wrap: wrap;
}

@media (max-width: 768px) {
  .stats-container {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .actions-container {
    flex-direction: column;
    align-items: center;
    
    .el-button {
      width: 200px;
    }
  }
}
</style>
